	// tirgul_081109.cpp : Defines the entry point for the console application.
	//

	//#include "stdafx.h"
	#include "string.h"
	#include "stdlib.h"
	#include "stdio.h"
	#include "conio.h"
	#include "windows.h"
	 
	typedef struct phone
	{
		char fname[10];
		char lname[10];
		char address[20];
		char number[10];
	}phoneBook;

	 
	 void menu(FILE *);
	 void add(FILE *);
	 int find(FILE *);
	 void change(FILE *);
	 void del(FILE *);
	 void printTheSelection(FILE *);
	 void printBook(FILE *);
	 
	 void main()
	 {
		FILE *pb;
		char fileName[20];
		printf("please enter the file name\n");//moti.bin
		scanf("%s",fileName);
		pb=fopen(fileName,"a+b");
		fclose(pb);
		pb=fopen(fileName,"r+b");
		menu(pb);
	 }
	////////////////////////////////////////////////////////

	 void menu(FILE *pb)
	 {
	   int select=1;
	   while(select)
	   {
		printf("\nSelect what u want to do:\n\n");
		printf("To add new record press 1.\n\n");
		printf("To change new record press 2.\n\n");
		printf("To delete record press 3.\n\n");
		printf("To find new record press 4.\n\n");
		printf("To print the all phone book records press 5.\n\n");
		printf("To exit press 0.\n\n");
		do
		{
			printf("please select 0-5 : ");
			scanf("%d",&select);
		}while ((select<0)||(select>5));

		   switch(select)
		   {
		   case 1: add(pb);
			   break;
		  case 2: change(pb);
			   break;
		   case 3: del(pb);
			   break;
		   case 4:	printTheSelection(pb);
			   break;
		   case 5:	printBook(pb);
			   break;
		   case 0: printf("good bye!\n\n");
		   }
	   }
		fclose(pb);
	 }
		

	 
	////////////////////////////////////////////////////////

	 void add(FILE *pb)
	 {
		phoneBook newRecord;
		fseek(pb,0,SEEK_END);
		flushall();
		printf("\nplease enter the record first name: ");
		gets(newRecord.fname);
		flushall();
		printf("\nplease enter the record last name: ");
		gets(newRecord.lname);
		flushall();
		printf("\nplease enter the record address: ");
		gets(newRecord.address);
		flushall();
		printf("\nplease enter the record number: ");
		gets(newRecord.number);
		fwrite(&newRecord,sizeof(phoneBook),1,pb);
		printf("\n");
		menu(pb);
	 }


	////////////////////////////////////////////////////////

	 int find(FILE *pb)
	 {
		 phoneBook buf;
		 int choose=0;
		 char findValue[20];
		 printf("Enter 1 if u want to look for a phone item \n");
		 printf("Enter 2 if u want to look for first name \n");
		 printf("Enter 3 if u want to look for last name \n");
		 printf("Enter 4 if u want to look for address item \n");
		 scanf("%d",choose);
		 rewind(pb);
		 switch(choose)
		 {
		 case 1: flushall();
			 printf("please enter the number of the person you want to look for: ");			
			 scanf("%s",&findValue);
			 while (fread(&buf,sizeof(phoneBook),1,pb))
				 if(!strcmp(findValue,buf.number))
					return 1;
			 return 0;
			 break;
			 
		 case 2:
			 printf("please enter the first name of the person you want to look for: ");			
			 scanf("%s",&findValue);
			 while (fread(&buf,sizeof(phoneBook),1,pb))
				 if(!strcmp(findValue,buf.fname))
					 return 1;
			 return 0;
			 break;

		 case 3: flushall();
			 printf("please enter the last name of the person you want to look for: ");			
			 scanf("%s",findValue);
			 while (fread(&buf,sizeof(phoneBook),1,pb))
				 if(!strcmp(findValue,buf.lname))
					 return 1;
			 return 0;
			 break;

		  case 4: flushall();
			 printf("please enter the address of the person you want to look for: ");			
			 scanf("%s",findValue);
			 while (fread(&buf,sizeof(phoneBook),1,pb))
				 if(!strcmp(findValue,buf.address))
					 return 1;
			 return 0;
			 break;
				
		 default: printf("\nyou printed a wrong choice\n\n");
			 break;
		 }
		 menu(pb);
	 }

	////////////////////////////////////////////////////////

	 void change(FILE *pb)
	 {
		    
			phoneBook newValue;
			printf("In order to change:\n ");
			if(find(pb))
			{
				printf("\nplease enter the new values: \n");
				printf("the new first name (or the same) is: ");
				scanf("%s",newValue.fname);
				printf("\nthe new last name (or the same) is: ");
				scanf("%s",newValue.lname);
				printf("\nthe new number (or the same) is: ");
				scanf("%s",newValue.number);
				printf("\nthe new address (or the same) is: ");
				scanf("%s",newValue.address);
				fseek(pb,-sizeof(phoneBook),SEEK_CUR);
				fwrite(&newValue,sizeof(phoneBook),1,pb);
			}
			else
				printf("\nthere is not such record!\n");
			menu(pb);
	 }

	////////////////////////////////////////////////////////

	 void del(FILE *pb)
	 {
		phoneBook delVal;
		int res;
		printf("In order to delete: \n ");
		if(find(pb))
		{
			res=fseek(pb,-sizeof(phoneBook),SEEK_END);
			fread(&delVal,sizeof(phoneBook),1,pb);
			fseek(pb,-sizeof(phoneBook),SEEK_CUR);
			fwrite(&delVal,sizeof(phoneBook),1,pb);
			fclose(pb);
			HANDLE h=CreateFile(TEXT("moti.bin"),GENERIC_WRITE|GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
			SetFilePointer(h,-sizeof(phoneBook),NULL,FILE_END);
			SetEndOfFile(h);
			CloseHandle(h);
			pb=fopen("moti.bin","r+b");

		}
		else
			printf("there is not such record!\n");
		menu(pb);
	 }

	 ////////////////////////////////////////////////////////

	 void printTheSelection(FILE * pb)
	 {
		phoneBook foundVal;
		if(find(pb))
		{
			fseek(pb,-sizeof(phoneBook),SEEK_CUR);
			fread(&foundVal,sizeof(phoneBook),1,pb);
			printf("the name is: %s %s ,the address is: %s and the number is: %s \n",foundVal.fname,foundVal.lname,foundVal.address,foundVal.number);
		}
		else
			printf("there is not such record!\n");

		menu(pb);
	 }


	 		  
	 ////////////////////////////////////////////////////////

	 void printBook(FILE *pb)
	 {
		 phoneBook curr;
		 rewind(pb);
		 while(fread(&curr,sizeof(phoneBook),1,pb))
			 printf("name: %s %s address: %s number: %s \n",curr.fname,curr.lname,curr.address,curr.number);
		 printf("/n");
		 menu(pb);
	 }

